package com.tbm.console.proj.json;

import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.proj.entity.ReportConfigInfoEntity;
import com.tbm.console.proj.service.ReportConfigInfoService;
import com.tbm.console.util.DateUtil;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//说明 字符串：ReportConfigInfo，作为替换字符串 LOWERReportConfigInfo

@RestController
public class ReportConfigInfoJson extends BaseController{

    @Autowired
    private ReportConfigInfoService reportConfigInfoService;
    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/reportConfigInfo-list/{page}/{size}")
    public Map<String,Object> reportConfigInfoList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = this.getPageData();
        pageData.put("deleteFlag", "N");
        long count = reportConfigInfoService.count(pageData);
        List<ReportConfigInfoEntity> reportConfigInfos = reportConfigInfoService.findAll(pageData,page-1,size);
        pageData.put("total",count);
        pageData.put("rows", reportConfigInfos);
        return pageData;
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/reportConfigInfo-ins-upd-del")
    public Object logCreateUpdate(){
        PageData pageData = this.getPageData();
        List<ReportConfigInfoEntity> reportConfigInfoList = reportConfigInfoService.findAll();
        ReportConfigInfoEntity reportConfigInfoEntity = null;
        if(reportConfigInfoList.size()==1){
            reportConfigInfoEntity = reportConfigInfoList.get(0);
        }else {
            reportConfigInfoEntity = new ReportConfigInfoEntity();
            for(ReportConfigInfoEntity entity : reportConfigInfoList){
                reportConfigInfoService.deleteById(entity.getId());
            }
        }
        reportConfigInfoEntity.setHost(pageData.getString("host"));
        reportConfigInfoEntity.setPort(Integer.valueOf((String) pageData.get("port")));
        reportConfigInfoEntity.setDeleteFlag(InsUpdDelFlagConstant.LOCALLOGDELETEFLAG);
        reportConfigInfoEntity.setPath(pageData.getString("path"));
        reportConfigInfoEntity.setFirstParam(pageData.getString("firstParam"));
        reportConfigInfoEntity.setCreateTime(DateUtil.getTime());
        reportConfigInfoService.save(reportConfigInfoEntity);
        return null;
    }
}